library(rgeos)
library(rgdal)
library(maptools)
library(sp)
library(spdep)

## REPLICATION CODE FOR MAYORAL ELECTION RESULTS

## Note that variable names are abreviated due to restrictions on ESRI Shapefile files
## Variables: 
## s__2008: Sanders vote share from 2008
## s__2005: Lagged Sanders vote share (from 2005)
## potpre: Pre-election potholes
## potpost: Post-election potholes

#Set Directory Below
setwd("...\\Replication\\Mayor Replication")
consols_df<-readOGR(dsn=".",layer="mayor_replication")

nb <- dnearneigh(coordinates(consols_df), 0, 5280*100)
nbd <- nbdists(nb, coordinates(consols_df))
gl2 <- lapply(nbd, function(x) 1/(x^2))
lw <- nb2listw(nb, glist=gl2)

sldv.fit<-lagsarlm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre, listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
error.fit<-errorsarlm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre, listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
fit<-lm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre)

sldv.fit.placebo<-lagsarlm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre+consols_df$potpost, listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
error.fit.placebo<-errorsarlm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre+consols_df$potpost, listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
fit.placebo<-lm(consols_df$s__2008~consols_df$s__2005+consols_df$potpre+consols_df$potpost)

#Mayoral Election Results (Table A.5, Model 1)
summary(fit)

#Mayoral Election Results (Table A.5, Model 2)
summary(error.fit)

#Mayoral Election Results Table A.5, (Model 3)
summary(sldv.fit)

#Placebo Test: Mayoral Election Results (Table A.7, Model 1)
summary(fit.placebo)

#Placebo Test: Mayoral Election Results (Table A.7, Model 2)
summary(error.fit.placebo)

#Placebo Test: Mayoral Election Results (Table A.7, Model 3)
summary(sldv.fit.placebo)


## REPLICATION CODE FOR CITY COUNCIL ELECTION RESULTS

## Note that variable names are abreviated due to restrictions on ESRI Shapefile files
## Variables: 
## i__2010: Incumbent city council vote share from 2010
## i__2006: Lagged incumbent city council vote share (from 2006)
## potpre: Pre-election potholes
## potpost: Post-election potholes
## D: City council district number

#Set Directory Below
setwd("...\\Replication\\Council Replication")
consols_df<-readOGR(dsn=".",layer="council_replication")

nb <- dnearneigh(coordinates(consols_df), 0, 5280*100)
nbd <- nbdists(nb, coordinates(consols_df))
gl2 <- lapply(nbd, function(x) 1/(x^2))
lw <- nb2listw(nb, glist=gl2)

sldv.fit<-lagsarlm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+factor(consols_df$D), listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
error.fit<-errorsarlm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+factor(consols_df$D), listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
fit<-lm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+factor(consols_df$D))

sldv.fit.placebo<-lagsarlm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+consols_df$potpost+factor(consols_df$D), listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
error.fit.placebo<-errorsarlm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+consols_df$potpost+factor(consols_df$D), listw=lw, method="eigen",quiet=FALSE, zero.policy=TRUE)
fit.placebo<-lm(consols_df$i__2010~consols_df$i__2006+consols_df$potpre+consols_df$potpost+factor(consols_df$D))

#City Council Election Results (Table A.6, Model 1)
summary(fit)

#City Council Election Results (Table A.6, Model 2)
summary(error.fit)

#City Council Election Results Table A.6, (Model 3)
summary(sldv.fit)

#Placebo Test: City Council Election Results (Table A.8, Model 1)
summary(fit.placebo)

#Placebo Test: City Council Election Results (Table A.8, Model 2)
summary(error.fit.placebo)

#Placebo Test: City Council Election Results (Table A.8, Model 3)
summary(sldv.fit.placebo)
